import * as moment from 'moment';
import {}from '../config/Config';
import {access,mkdir} from 'fs';
import { PuppteerPage } from '../config/Config'

/**
 * 获取时间
 * @param format 
 */
export function get_current_date(format:string='YYYY-MM-DD HH:mm:ss'){
    return moment(Date.now()).format(format);
}
/**
 * 获取截图路径
 * @param ImageName 
 * @param Dir 
 * @param Type 
 */
export function get_shot_photo_store_path(ImageName:string, Dir:string=PuppteerPage.ShotDir, Type:'.jpeg'|'.png'='.png'):Promise<string>{
    let reg = /^\w+\/\w+\/$/g;
    let index = reg.exec(Dir);

    if(!index){
        throw '截图dir格式不正确，**/'
    }

    return new Promise((resolve,reject)=>{
        access(Dir,e=>{
            if(e){
                mkdir(Dir,e=>{
                    if(e){
                        reject ('文件目录生成失败');
                    }
                })
            }
            resolve(`${Dir}${ImageName}${Type}`);
        })
    })
}
/**
 * 拿到请求 url
 * @param Path 
 */
export function get_post_url(Path:string){
    return PuppteerPage.ServerPort+Path;
}
/**
 * 判断值是否在数组内
 * @param Array 
 * @param Value 
 */
export function is_in_array<T>(Array:T[],Value:any):boolean{

    for(let k in Array){
        if(Array[k]==Value){
            return true
        }
    }
    return false;
}
export function get_input(Placeholder:string){
    return `input[placeholder='${Placeholder}']`;
}
/**
 * 数组排序，默认大到小
 * @param Value 
 * @param MaxToMin 
 */
export function sort_array(Value:any[],MaxToMin:boolean=true){
    if(MaxToMin){
        return Value.sort((a,b)=>{
            return b-a;
        })
    }else{
        return Value.sort((a,b)=>{
            return a-b;
        })
    }
}